package com.leyou.sms.utils;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.leyou.sms.config.SmsProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author syf
 * @version 1.0
 * @date 2019/08/12 15:15
 * @Description TODO:阿里短信工具类
 */
@Slf4j
@Component
public class SendMsgUtils {
    @Autowired
    private SmsProperties prop;

    @Autowired
    private IAcsClient client; //SmsConfiguration类中的方法

    public void sendCheckCodMsg(String phone,String code){

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain(prop.getDomain());
        request.setVersion(prop.getVersion());
        request.setAction(prop.getAction());
        request.putQueryParameter("RegionId", prop.getRegionID());
        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", prop.getSignName());
        request.putQueryParameter("TemplateCode", prop.getVerifyCodeTemplate());
        request.putQueryParameter("TemplateParam", "{\"code\":\""+code+"\"}");
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ServerException e) {
            log.error("阿里巴巴之阿里错误");
            e.printStackTrace();
        } catch (ClientException e) {
            log.error("阿里巴巴之自己短信错误");
            e.printStackTrace();
        }
    }
}
